package com.example.demo2.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo2.common.Result;
import com.example.demo2.entity.DTO.QuestionDTO;
import com.example.demo2.entity.Question;
import com.example.demo2.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @PostMapping("/add")
    public Result save(@RequestBody Question question) {
        question.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        boolean save = questionService.save(question);
        if(question.getQId() == -1){
            LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Question::getQId,question.getQId());
            Question one = questionService.getOne(lqw);
            one.setQId(one.getId());
            questionService.updateById(one);
        }
        if (save) {
            return Result.success(save);
        } else {
            return Result.error("-1", save);
        }
    }
}
